package com.xhapi.service.impl;

import com.xhapi.mapper.neoMapper.ChapterMapper;
import com.xhapi.mapper.neoMapper.CourseMapper;
import com.xhapi.mapper.neoMapper.PointMapper;
import com.xhapi.mapper.neoMapper.SmallChapterMapper;
import com.xhapi.pojo.neo4j.entity.Chapter;
import com.xhapi.pojo.neo4j.entity.Course;
import com.xhapi.pojo.neo4j.entity.Point;
import com.xhapi.pojo.neo4j.entity.SmallChapter;
import com.xhapi.pojo.neo4j.vo.Categories;
import com.xhapi.pojo.neo4j.vo.EchartsShow;
import com.xhapi.pojo.neo4j.vo.Links;
import com.xhapi.pojo.neo4j.vo.Nodes;
import com.xhapi.service.Neo4jGeoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
@Slf4j
public class Neo4jGeoServiceImpl implements Neo4jGeoService {
    private final CourseMapper courseMapper;
    private final ChapterMapper chapterMapper;
    private final SmallChapterMapper smallChapterMapper;
    private final PointMapper pointMapper;

    @Override
    public EchartsShow getAllMap() {
        // node link category
        List<Nodes> nodes = new ArrayList<>();
        List<Course> courseList = courseMapper.findAll();
        List<Chapter> chapterList = chapterMapper.findAll();
        List<SmallChapter> smallChapterList = smallChapterMapper.findAll();
        List<Point> pointList = pointMapper.findAll();

        // 为charts添加的所有节点
        courseList.forEach(course -> nodes.add(new Nodes(course.getId(), course.getName(), 50.0, 0.0, 1)));
        chapterList.forEach(chapter -> nodes.add(new Nodes(chapter.getId(), chapter.getName(), 30.0, 0.0, 2)));
        smallChapterList.forEach(smallChapter -> nodes.add(new Nodes(smallChapter.getId(), smallChapter.getName(), 20.0, 0.0, 3)));
        Double defaultValue=15.0;

        pointList.forEach(point -> nodes.add(new Nodes(point.getId(), point.getName(), defaultValue-(point.getValue()/2), (double) point.getValue(), 4)));

        // 为charts添加的所有关系
        List<Links> links = new ArrayList<>();
        courseList.forEach(course -> {
            Set<Chapter> chapterSet = course.getChapterSet();
            chapterSet.forEach(chapter -> {
                links.add(new Links(course.getId().toString(), chapter.getId().toString()));
            });
        });
        chapterList.forEach(chapter -> {
            Set<SmallChapter> smallChapterSet = chapter.getSmallChapterSet();
            smallChapterSet.forEach(smallChapter -> {
                links.add(new Links(chapter.getId().toString(), smallChapter.getId().toString()));
            });
        });
        smallChapterList.forEach(smallChapter -> {
            Set<Point> pointSet = smallChapter.getPointSet();
            pointSet.forEach(point -> {
                links.add(new Links(smallChapter.getId().toString(), point.getId().toString()));
            });
        });

        // 为charts添加的种类
        List<Categories> category = new ArrayList<>();
        category.add(new Categories(""));
        category.add(new Categories("必修"));
        category.add(new Categories("章节"));
        category.add(new Categories("小节"));
        category.add(new Categories("知识点"));
        return new EchartsShow(nodes, links, category);
    }

    @Override
    public void updateMap(List<String> nameList) {
        nameList.forEach(name -> {
            pointMapper.updateValueByName(name);
            log.info("更新了" + name + "的值");
        });
    }
}
